﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using System.Data.SqlClient;
using System.Data;

namespace BLL
{
    public class TRIEUCHUNGBLL
    {
        DataService DB = new DataService();

        public void insert(string trieuchung, int mabenhan, DateTime ngaytao, string file)
        {
            string sql = "insert into trieuchung values (@trieuchung,@mabenhan,@ngaytao,@file)";
            SqlParameter paraTrieuchung = new SqlParameter("trieuchung", trieuchung);
            SqlParameter paraMaba = new SqlParameter("mabenhan", mabenhan);
            SqlParameter paraNgay = new SqlParameter("ngaytao", ngaytao);
            SqlParameter paraFile = new SqlParameter("file", file);

            DB.ExecuteNonQuery(sql, paraTrieuchung, paraMaba, paraNgay, paraFile);
        }

        public void update(int matrieuchung, string trieuchung, int mabenhan, DateTime ngaytao, string file_patch)
        {
            string sql = "update trieuchung set trieuchung = N'" + trieuchung + ", mabenhan = '" + mabenhan.ToString() + "', ngaytao = '" + ngaytao.ToString() + "' , file_patch = '" + file_patch + "' where matrieuchung = " + matrieuchung.ToString();
            DB.ExecuteNonQuery(sql);
        }

        public void delete(int matrieuchung)
        {
            string sql = "delete from trieuchung where matrieuchung = " + matrieuchung.ToString();
            DB.ExecuteNonQuery(sql);
        }

        public TRIEUCHUNG getTrieuchungWithIDBaiviet(int idBaiviet)
        {
            string sql = "select tch.matrieuchung, tch.trieuchung, tch.mabenhan, tch.ngaytao, tch.file_patch "
                        + " from baiviet bv,hosobenhan hsba, trieuchung tch where bv.mabaiviet = " + idBaiviet.ToString();

            DataTable table = new DataTable();
            table = DB.ExecuteReader(sql);

            TRIEUCHUNG tch = new TRIEUCHUNG();
            DataRow r = table.Rows[0];

            tch.matrieuchung = Int32.Parse(r[0].ToString());
            tch.trieuchung = r[1].ToString();
            tch.mabenhan = Int32.Parse(r[2].ToString());
            tch.ngaytao = Convert.ToDateTime(r[3].ToString());
            try { tch.file_patch = r[4].ToString(); }
            catch { tch.file_patch = ""; }

            return tch;
        }

        public void updateTrieuchungContentWithId(int idTrieuchung, string trieuchung)
        {
            string sql = "update trieuchung set trieuchung = N'" + trieuchung + "' where matrieuchung = " + idTrieuchung.ToString();
            DB.ExecuteNonQuery(sql);
        }
    }
}
